package cnm.radio;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.util.Iterator;
import java.util.List;
import java.util.ArrayList;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.text.SimpleDateFormat;
import java.text.ParseException;
import java.util.Date;

import cnm.red5.CnmPlayItem;
import cnm.red5.CnmServerStream;
import cnm.red5.CnmServerStreamManager;

import org.red5.server.WebScope;

public class CnmRadioService
implements  Runnable {
	private Thread worker;
	private WebScope webScope;
	/*private CnmServerStreamManager cnmServerStreamManager;
	private ExecutorService inputStreamExecutor = Executors.newFixedThreadPool(2);
	private List<CodecTaskListener> listeners = new CopyOnWriteArrayList<CodecTaskListener>();
	private LinkedBlockingQueue<CodecTask> taskQueue = new LinkedBlockingQueue<CodecTask>();
	volatile private boolean isStdOutRunning;
	volatile private boolean isStdErrRunning;
	private Pattern durationPattern;
	private Pattern processPattern;
	
	public void setCnmServerStreamManager(CnmServerStreamManager cnmServerStreamManager) {
		this.cnmServerStreamManager = cnmServerStreamManager;
	}*/

	public void setWebScope(WebScope webScope) {
		this.webScope = webScope;
	}
	
	public void init() {
		worker = new Thread(this);
		worker.setName("CNM Radio");
		worker.setDaemon(true);
		worker.start();
	}	
	
	@SuppressWarnings("unchecked")
	public void run() {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd,HH:mm:ss");
		try {
			Date date1 = df.parse("2008-04-28,15:16:00");
			Date date2 = df.parse("2008-04-28,15:16:30");
			
			CnmPlayItem item1 = new CnmPlayItem();
			item1.setStartDate(date1);
			item1.setName("test1.mp3");
			CnmPlayItem item2 = new CnmPlayItem();
			item2.setStartDate(date2);
			item2.setName("test2.mp3");	
			ArrayList list = new ArrayList();
			list.add(item1);
			list.add(item2);
			
			CnmServerStream serverStream = new CnmServerStream(list);
			serverStream.setName("live_radio");
			serverStream.setPublishedName("live_radio");
			serverStream.setScope(webScope);
			serverStream.start();
			System.out.println("CnmRadioService started...");
		} catch(ParseException e ) {
			//TODO
		}	
		
		/*while (true) {
			try {

			} catch (Exception e) {
				// do nothing
			}
		}*/
	}
	

}
